package com.example.s.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ScrollView;

import androidx.core.widget.NestedScrollView;

public class MyScrollView extends NestedScrollView {
    private String TAG = "MyScrollView";
    private OnScrollToTopOrBottomListener listener;

    public void setOnScrollToTopOrBottomListener(OnScrollToTopOrBottomListener listener) {
        this.listener = listener;
    }

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (listener == null)
            return;
        boolean b = canScrollVertically(1);//返回false表示不能往上滑动，即代表到底部了；
        if (!b) {
            Log.d(TAG, "滑动到底部");
            listener.toBottom();
        }
        boolean b1 = canScrollVertically(-1);//返回false表示不能往下滑动，即代表到顶部了；
        if (!b1) {
            Log.d(TAG, "滑动到顶部");
            listener.toTop();
        }
    }

    public interface OnScrollToTopOrBottomListener {
        void toTop();

        void toBottom();
    }
}
